package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.SelectUserAddressActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserAddressBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.SetUserAddressModel;

public class AddressAdapter extends BaseAdapter<UserAddressBean> {
    private String userid,useraddressid;

    public AddressAdapter(Context context, List<UserAddressBean> items, int layoutResource,String userid,String useraddressid){
        super(context, items, layoutResource);
       this.userid = userid;
       this.useraddressid = useraddressid;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        AddressViewHolder viewHolder = new AddressViewHolder(itemView);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        AddressViewHolder viewHolder=(AddressViewHolder) holder;

        UserAddressBean userAddressBean=items.get(position);

          final String username = userAddressBean.getUseraddress_username();
          final String phone = userAddressBean.getUseraddress_phone();

          final String address = userAddressBean.getUseraddress_city()+ userAddressBean.getUseraddress_address();
          final String addressid = userAddressBean.getUseraddress_id();

       // Toast.makeText(context,userid+"---"+addressid,Toast.LENGTH_SHORT).show();
          viewHolder.addressitem_address.setText(address);
          viewHolder.addressitem_phone.setText(phone);
          viewHolder.addressitem_username.setText(username);


          if (useraddressid != null){
              if (useraddressid.equals(addressid)){
                  viewHolder.addressitem_flag.setChecked(true);
              }
          }
          else {

          }



                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                SetUserAddressModel setUserAddressModel = new SetUserAddressModel();
                setUserAddressModel.setUserAddress(userid,addressid,successBeanTListener);

            }
        });
    }

    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
           // Toast.makeText(context,"错误",Toast.LENGTH_SHORT).show();
            String success = successBean.getSuccess();
            if (success.equals("1")){
                Toast.makeText(context,"选择成功",Toast.LENGTH_SHORT).show();

                if(SelectUserAddressActivity.class.isInstance(context)){

                 SelectUserAddressActivity activity = (SelectUserAddressActivity)context;
                    activity.finish();}

            }
            else{
                Toast.makeText(context,"选择失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context,"错误",Toast.LENGTH_SHORT).show();
        }
    };
}
